//
//  SetOpenTimeViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/26.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SetOpenTimeViewModel: BaseViewModel {
    var starTime:String = "00:00" {
        didSet {
           model.openTime = starTime
        }
    }
    var closeTime:String = "23:59" {
        didSet {
            model.endTime = closeTime
        }
    }
    var isUpdate:Bool = false //是否更新
    
  lazy var model:TimeModel = {
    let model =  TimeModel()
    return model
  }()
    
    override init() {
        super.init()
        
    }
    
    func changeModel() {
        starTime = model.openTime
        closeTime = model.endTime
    }
    
    /// 判断时间当前和结束时间做比对
    ///
    /// - Parameters:
    ///   - starBool: 是否是开始时间
    ///   - timeStr: 时间
    /// - Returns: 是否在时间段内
    func compareTime(starBool:Bool , timeStr:String) -> Bool {
        if starBool {
            return self.conformTime(startTime: timeStr, endTime: closeTime)
        }else {
            return self.conformTime(startTime: starTime, endTime: timeStr)
        }
     
    }
    
    private func conformTime(startTime:String, endTime:String) ->Bool{
        if starTime.count == 0 {
            if endTime == "0:00" {
                return false
            }
        }
        if startTime.count == 0 {
            return true
        }
        if endTime.count == 0 {
            return true
        }
        let startArr = (startTime as NSString).components(separatedBy: ":")
        if startArr.count < 2 {
            return false
        }
        let endArr = (endTime as NSString).components(separatedBy: ":")
        if endArr.count < 2 {
            return false
        }
        
        let starHour:String = startArr.first!
        let starMinute:String  = startArr.last!
        
        let endHour:String = endArr.first!
        let endMinute:String = endArr.last!
   
        if (starHour as NSString).intValue > (endHour as NSString).intValue {
            CYMBShow.showMessage(kText("结束时间不能小于开始时间"))
            return false
        }
        
        if (starHour as NSString).intValue == (endHour as NSString).intValue {
            if Int(starMinute)! > Int(endMinute)! {
                CYMBShow.showMessage(kText("结束时间不能小于开始时间"))
                return false
            }else {
                if Int(starMinute)! == Int(endMinute)! {
                    CYMBShow.showMessage(kText("结束时间不能和开始时间相同"))
                    return false
                }
                return true
            }
        }
        return true
    }
}
